package com.bysj.sausys.service.impl;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.beans.Pagination;
import com.bysj.sausys.common.UserConst;
import com.bysj.sausys.dao.SysAssMemberMapper;
import com.bysj.sausys.exception.ParamException;
import com.bysj.sausys.model.SysAssMember;
import com.bysj.sausys.service.ISysAssMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 社团会员Service
 *
 * @author WangZK
 */
@Service("sysAssMemberService")
public class SysAssMemberService implements ISysAssMemberService {

    @Resource
    private SysAssMemberMapper sysAssMemberMapper;


    /**
     * 保存社团会员信息
     *
     * @param sysAssMember
     * @param map
     */
    @Override
    public void save(SysAssMember sysAssMember, Map map) {
        //进行数据校验
        if (checkAssMemberExist(sysAssMember.getId(), sysAssMember.getStuNum(), UserConst.getCurrentAssId(map))) {
            throw new ParamException("社团会员已存在，保存失败");
        }

        sysAssMember.setAssId(UserConst.getCurrentAssId(map));
        sysAssMember.setInsertMan(UserConst.getCurrentUsername(map));
        sysAssMember.setInsertTime(new Date());
        sysAssMemberMapper.insertSelective(sysAssMember);
    }

    /**
     * 判断社团会员是否已存在
     *
     * @param id
     * @param stuNum
     * @param assId
     * @return
     */
    private boolean checkAssMemberExist(Integer id, String stuNum, Integer assId) {
        return sysAssMemberMapper.countByStuNumAndAssId(id, stuNum, assId) > 0;
    }

    /**
     * 更新社团会员信息
     *
     * @param sysAssMember
     * @param map
     */
    @Override
    public void update(SysAssMember sysAssMember, Map map) {
        //进行数据校验
        SysAssMember before = sysAssMemberMapper.selectByPrimaryKey(sysAssMember.getId());
        if (before == null) {
            throw new ParamException("待更新的干部信息不存在");
        }
        if (checkAssMemberExist(sysAssMember.getId(), sysAssMember.getStuNum(), UserConst.getCurrentAssId(map))) {
            throw new ParamException("社团会员已存在，更新失败");
        }

        SysAssMember after = new SysAssMember();

        after.setId(sysAssMember.getId());
        after.setAssId(UserConst.getCurrentAssId(map));
        after.setStuNum(sysAssMember.getStuNum());
        after.setName(sysAssMember.getName());
        after.setSex(sysAssMember.getSex());
        after.setCollege(sysAssMember.getCollege());
        after.setMajor(sysAssMember.getMajor());
        after.setRemarks(sysAssMember.getRemarks());
        after.setLastupdMan(UserConst.getCurrentUsername(map));
        after.setLastupdTime(new Date());

        sysAssMemberMapper.updateByPrimaryKeySelective(after);
    }

    /**
     * 分页获取社团会员列表
     *
     * @param assId
     * @param keyword
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult<Map> getPageByKeyword(Integer assId, String keyword, PageQuery pageQuery) {
        int count = sysAssMemberMapper.countByAssIdAndKeyword(assId, keyword);
        if (count > 0) {
            List<Map> sysAssMemberList = sysAssMemberMapper.getPageByAssIdAndKeyword(assId, keyword, pageQuery);
            PageResult<Map> result = new PageResult<>();
            result.setList(sysAssMemberList);
            Pagination pagination = new Pagination();
            pagination.setTotal(count);
            pagination.setPageSize(pageQuery.getPageSize());
            pagination.setCurrentPage(pageQuery.getPageNo());
            result.setPagination(pagination);
            return result;
        }
        return new PageResult<>();
    }

    /**
     * 根据id获取社团会员信息
     *
     * @param id
     * @return
     */
    @Override
    public SysAssMember findById(Integer id) {
        return sysAssMemberMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据id删除社团会员
     *
     * @param ids
     */
    @Override
    public void delete(Integer[] ids) {
        sysAssMemberMapper.batchDeleteByPrimaryKey(ids);
    }
}
